package com.ddxz.middleground.meta.service.impl;

import com.ddxz.middleground.meta.dto.MetaHeader;
import com.ddxz.middleground.meta.api.struct.source.Source;
import com.ddxz.middleground.meta.api.struct.source.SourceDetail;
import com.ddxz.middleground.meta.dto.source.SourceInfo;
import com.ddxz.middleground.meta.dto.source.SourcePage;
import com.ddxz.middleground.meta.entity.SourceEntity;
import com.ddxz.middleground.meta.mapper.ObjectMapper;
import com.ddxz.middleground.meta.mapper.SourceMapper;
import com.ddxz.middleground.meta.service.SourceService;
import com.ddxz.middleground.meta.util.MetaConstData;
import com.ddxz.tool.core.common.CommonUtil;
import com.ddxz.tool.core.convert.BeanUtil;
import com.ddxz.tool.core.convert.DdxzUtil;
import com.ddxz.tool.core.convert.StringUtil;
import com.ddxz.tool.web.dto.DdxzResponse;
import com.ddxz.tool.web.dto.PageResponse;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import static com.ddxz.middleground.meta.util.ErrorStatus.*;

/**
 * class description
 *
 * @author 朱宗刚
 * @version 1.0.0
 * @date 2020/8/21
 */

@Service
public class SourceServiceImpl implements SourceService, MetaConstData {
    @Override
    public DdxzResponse sourceSave(MetaHeader header, SourceInfo sourceInfo) {
        Integer num = mapper.getNumByUserAndName(header.getUserId(), sourceInfo.getName(), header.getTenant());
        CommonUtil.ddxzAssetGreat(num, 0, SOURCE_NAME_REPEAT);
        SourceEntity entity = BeanUtil.getTarget(sourceInfo, SourceEntity.class);
        if(StringUtil.isEmpty(sourceInfo.getId())) {
            entity.setId(CommonUtil.generatorId("ddxz_source"));
            entity.setCreator(header.getNickname());
            entity.setUserId(header.getUserId());
            mapper.saveSource(entity, header.getTenant());
        } else {
            mapper.modifySource(entity, header.getTenant());
        }

        return DdxzResponse.correct("数据源保存成功", entity.getId());
    }

    @Override
    public DdxzResponse sourceRemove(MetaHeader header, String sourceId) {
        Integer num = objectMapper.getNumBySourceId(sourceId, header.getTenant());
        CommonUtil.ddxzAssetGreat(num, 0, SOURCE_HAS_OBJECT);
        mapper.removeSource(sourceId, header.getTenant());
        return DdxzResponse.correct("数据源移除成功", sourceId);
    }

    @Override
    public DdxzResponse sourceList(MetaHeader header, SourcePage page) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        try(Page<SourceEntity> entityPage = isAllData(header.getRoleIds()) ?
                mapper.getSourceList(page.getClassify(), page.getStatus(), null, header.getTenant()) :
                mapper.getSourceList(page.getClassify(), page.getStatus(), header.getUserId(), header.getTenant())) {
            PageResponse<Source> result = PageResponse.getInstance(entityPage, Source.class, DdxzUtil.SECOND_DATE);
            return DdxzResponse.correct(result);
        } catch (Exception e) {
            logger.error("source search error", e);
            return DdxzResponse.error(SOURCE_SEARCH_ERROR);
        }
    }

    @Override
    public DdxzResponse sourceDetail(MetaHeader header, String sourceId) {
        SourceEntity entity = mapper.getSourceDetail(sourceId, header.getTenant());
        CommonUtil.ddxzAssetNull(entity, SOURCE_NOT_EXISTS);
        return DdxzResponse.correct(BeanUtil.getTarget(entity, SourceDetail.class, DdxzUtil.SECOND_DATETIME));
    }

    @Override
    public DdxzResponse sourceTest(MetaHeader header, String sourceId) {
        return null;
    }

    @Override
    public DdxzResponse setState(MetaHeader header, String sourceId, Integer state) {
        return null;
    }

    private final SourceMapper mapper;

    private final ObjectMapper objectMapper;

    private static final Logger logger = LoggerFactory.getLogger(SourceServiceImpl.class);

    public SourceServiceImpl(SourceMapper mapper, ObjectMapper objectMapper) {
        this.mapper = mapper;
        this.objectMapper = objectMapper;
    }
}
